import { boot } from 'quasar/wrappers'
import axios from 'axios'
import { Notify, Loading } from "quasar";

// Be careful when using SSR for cross-request state pollution
// due to creating a Singleton instance here;
// If any client changes this (global) instance, it might be a
// good idea to move this instance creation inside of the
// "export default () => {}" function below (which runs individually
// for each client)
const api = axios.create({ baseURL: 'http://dev.bingzunkeji.com' })

export default boot(({ app, store }) => {
    // for use inside Vue files (Options API) through this.$axios and this.$api

    app.config.globalProperties.$axios = axios
        // ^ ^ ^ this will allow you to use this.$axios (for Vue Options API form)
        //       so you won't necessarily have to import axios in each vue file

    app.config.globalProperties.$api = api
        // ^ ^ ^ this will allow you to use this.$api (for Vue Options API form)
        //       so you can easily perform requests against your app's API

    api.interceptors.request.use(
        async config => {
            if (config.loading !== false) {
                Loading.show();
            }
            // config.baseURL = store.getters["common/baseURL"];
            // let token = await store.dispatch("auth/getToken");
            // if (token) {
            //     config.headers["token"] = token;
            // }
            return config;
        },
        error => {
            return Promise.reject(error);
        }
    );

    //  响应拦截
    api.interceptors.response.use(
        response => {
            Loading.hide();
            let { code, data, msg } = response.data;
            if (code == 1) {
                return Promise.resolve(data);
            } else {
                if (msg && response.config.notify !== false) {
                    Notify.create({
                        type: "error",
                        message: msg
                    });
                }
                return Promise.reject(msg);
            }
        },
        async error => {
            Loading.hide();
            if (!error.response) {
                return Promise.reject(error);
            }
            let { status, data } = error.response;
            let { msg } = data;

            switch (status) {
                case 401:
                    // 未登录
                    // let ret = await store.dispatch("auth/toAuth", router.currentRoute);
                    // if (router.currentRoute.path !== ret.path) {
                    //     router.push(ret);
                    // }
                    break;
                case 403:
                    // 权限不足;
                    // msg = msg || "权限不足!";
                    // Notify.create({
                    //     message: msg,
                    //     color: "negative",
                    //     icon: "warning"
                    // });
                    break;
                default:
                    msg = msg || status + " 服务器异常，请稍候重试!";
                    Notify.create({
                        message: msg,
                        color: "negative",
                        icon: "warning"
                    });
                    break;
            }
            return Promise.reject(msg);
        }
    );
})

export { api }